<?php
namespace App\Service;
use Firebase\JWT\JWT;

class Token
{
    protected $key;
    public function __construct()
    {
        $this->key = 'lizichen';
    }
    /**
     * 生成token
     */
    public function createToken($id)
    {
        $time = time();
        $payload = array(
            "iss" => "",
            "aud" => "",
            "iat" => $time,
            "nbf" => $time,
            "exp" => $time+86400,
            "id" => $id
        );

        $token = JWT::encode($payload,$this->key);

        return $token;
    }

    /**
     * 验证token
     */
    public function validateToken($token)
    {
        try {
            $decoded = JWT::decode($token, $this->key, array('HS256'));
            return $decoded->id;
        }catch (\Exception $e){
            return 'token过期';
        }
    }
}
